package nc.appcan.sp.bs.tool;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Servlet;

import nc.appcan.sppub.pub.MAAction;
import nc.appcan.sppub.pub.MABillAction;
import nc.appcan.sppub.pub.MABillFunAction;
import nc.appcan.sppub.pub.MAFileAction;
import nc.bs.framework.common.RuntimeEnv;
import nc.itf.uap.pf.AppCanBillItf;
import nc.itf.uap.pf.IAppCanSpItf;
import nc.itf.uap.pf.INCMAFileInter;
import nc.itf.uap.pf.INCVOHandler;

public class AnnotateTool {
	public static final String NCHOME =RuntimeEnv.getInstance().getNCHome()+"/modules/uap/META-INF/classes/";
	
	static {
		initService();
		initFileService();
		initBillService();
		initBillFuncService();
	}
	public static AppCanBillItf  getServiceBillClass(String functype){
		AppCanBillItf itf = ServiceCache.INSTANCE.getBillfuncmap().get(functype);
		return itf;
	}
	
	public static IAppCanSpItf  getServiceClass(String functype){
		IAppCanSpItf itf = ServiceCache.INSTANCE.getServicemap().get(functype);
		return itf;
	}
	
	public static INCMAFileInter  getFileServiceClass(String functype){
		INCMAFileInter itf = ServiceCache.INSTANCE.getFileservicemap().get(functype);
		if(itf==null)itf = ServiceCache.INSTANCE.getFileservicemap().get("common");
		return itf;
	}
	public static INCVOHandler  getBillServiceClass(String functype){
		INCVOHandler itf = ServiceCache.INSTANCE.getBillservicemap().get(functype);
		if(itf==null)itf = ServiceCache.INSTANCE.getBillservicemap().get("common");
		return itf;
	}
	private static void initBillService(){
		try {
			List<Class<?>> list = getClasses("nc.appcan.sp.bs.ncbill");
			for (Class<?> cla:list){
				String classname = cla.getName();
				if(classname.contains("$"))continue;
				if(cla.newInstance() instanceof INCVOHandler){
					MABillAction  action =cla.getAnnotation(MABillAction.class);
					if(action!=null)ServiceCache.INSTANCE.getBillservicemap().put(action.value(), (INCVOHandler)cla.newInstance());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void initBillFuncService(){
		try {
			List<Class<?>> list = getClasses("nc.appcan.bill.function");
			for (Class<?> cla:list){
				String classname = cla.getName();
				if(classname.contains("$"))continue;
				if(cla.newInstance() instanceof AppCanBillItf){
					MABillFunAction action =cla.getAnnotation(MABillFunAction.class);
					if(action!=null)ServiceCache.INSTANCE.getBillfuncmap().put(action.value(), (AppCanBillItf)cla.newInstance());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void initService(){
		try {
			List<Class<?>> list = getClasses("nc.appcan.sp.bean");
			for (Class<?> cla:list){
				String classname = cla.getName();
				if(classname.contains("$"))continue;
				if(cla.newInstance() instanceof IAppCanSpItf){
					MAAction  action =cla.getAnnotation(MAAction.class);
					if(action!=null)ServiceCache.INSTANCE.getServicemap().put(action.value(), (IAppCanSpItf)cla.newInstance());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void initFileService(){
		try {
			List<Class<?>> list = getClasses("nc.appcan.sp.bs.filelist");
			for (Class<?> cla:list){
				if(cla.newInstance() instanceof INCMAFileInter){
					MAFileAction  action =cla.getAnnotation(MAFileAction.class);
					if(action!=null)ServiceCache.INSTANCE.getFileservicemap().put(action.value(), (INCMAFileInter)cla.newInstance());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 private  static List<Class<?>> getClasses(String packagename) throws Exception{  
		
		 String url = NCHOME+packagename.replace(".", "/"); 
		 List<Class<?>> list = new ArrayList<Class<?>>();  
		 File dir =  new File(url);
		 if (!dir.exists()) return list;   
        for (File f : dir.listFiles()) {  
            String name = f.getName();  
            if (name.endsWith(".class")) {  
            	list.add(Class.forName(packagename + "." + name.substring(0, name.length() - 6)));  
            }  
        } 
	       return list;  
	 }  
	
}
